﻿using System;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using WINAPI.User32;

namespace WeaponEvalue.Logic
{
    public class UIOperator
    {
        public static void OperateButton(IntPtr handle)
        {
            while (User32Dll.SendMessage(handle, User32Dll.WM_LBUTTONDOWN, (IntPtr) User32Dll.MK_LBUTTON,
                                         User32Dll.MakeLParam(20, 20)) != 0)
            {
                Thread.Sleep(50);
            }
            User32Dll.SendMessage(handle, 0x00F5, new IntPtr(0), new IntPtr(0));
            User32Dll.SendMessage(handle, User32Dll.WM_KILLFOCUS, new IntPtr(0), new IntPtr(0));
        }

        public static void OperateButton(IntPtr handle, int buttonId)
        {
            User32Dll.SendDlgItemMessage(handle, buttonId, User32Dll.BM_CLICK, new IntPtr(0), new IntPtr(0));
        }

        public static void OperateDropDown(IntPtr handle, int value)
        {
           while( User32Dll.SendMessage(handle, User32Dll.WM_SETFOCUS, IntPtr.Zero, IntPtr.Zero)!=0)
           {
               Thread.Sleep(50);
           }
            User32Dll.SendMessage(handle, User32Dll.CB_SHOWDROPDOWN, new IntPtr(1), new IntPtr(0)); //打开列表
            while( User32Dll.SendMessage(handle, User32Dll.CB_SETCURSEL, new IntPtr(value), IntPtr.Zero)==-1){
                Thread.Sleep(50);//选择 
            }
            while( User32Dll.SendMessage(handle, User32Dll.WM_KEYDOWN, (IntPtr)Vks.VK_RETURN, IntPtr.Zero)!=0){
                Thread.Sleep(50); //响应回车键 
            }
            User32Dll.SendMessage(handle, User32Dll.CB_SHOWDROPDOWN, IntPtr.Zero, IntPtr.Zero); //关闭列表  
            User32Dll.SendMessage(handle, User32Dll.WM_KILLFOCUS, IntPtr.Zero, IntPtr.Zero);
        }

        public static void OperateDropDown(IntPtr handle, string value)
        {
            while (User32Dll.SendMessage(handle, User32Dll.WM_SETFOCUS, IntPtr.Zero, IntPtr.Zero) != 0)
            {
                Thread.Sleep(50);
            }
            User32Dll.SendMessage(handle, User32Dll.CB_SHOWDROPDOWN, new IntPtr(1), new IntPtr(0)); //打开列表
            while (User32Dll.SendMessage(handle, User32Dll.CB_SELECTSTRING, new IntPtr(-1), Marshal.StringToHGlobalAnsi(value)) == -1)
            {
                Thread.Sleep(50);//选择 
            }
            while (User32Dll.SendMessage(handle, User32Dll.WM_KEYDOWN, (IntPtr)Vks.VK_RETURN, IntPtr.Zero) != 0)
            {
                Thread.Sleep(50); //响应回车键 
            }
            User32Dll.SendMessage(handle, User32Dll.CB_SHOWDROPDOWN, IntPtr.Zero, IntPtr.Zero); //关闭列表  
            User32Dll.SendMessage(handle, User32Dll.WM_KILLFOCUS, IntPtr.Zero, IntPtr.Zero);
        }

        public static string GetText(IntPtr handle)
        {
            var stringBuilder = new StringBuilder(100);
            User32Dll.GetWindowText(handle, stringBuilder, 100);
            return stringBuilder.ToString();
        }

    }
}
